﻿using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Web;
using DiarySocial.Models;

namespace DiarySocial.DAL
{
    public class UnitOfWork : IDisposable
    {
        private DiaryEnties context = new DiaryEnties();
        // User profile
        private GenericRepository<UserProfile> _userRepository;

        public GenericRepository<UserProfile> UserRepository
        {
            get { return _userRepository ?? (_userRepository = new GenericRepository<UserProfile>(context)); }
        }
        // Diary
        private DiaryRepository _diaryRepository;
        public DiaryRepository DiaryRepository
        {
            get { return _diaryRepository ?? (_diaryRepository = new DiaryRepository(context)); }
        }
        // category
        private GenericRepository<Category> _categoryRepository;

        public GenericRepository<Category> Category
        {
            get { return _categoryRepository ?? (_categoryRepository = new GenericRepository<Category>(context)); }
        }
        // comment
        private GenericRepository<Comment> _commentRepository;

        public GenericRepository<Comment> CommentRepository
        {
            get { return _commentRepository ?? (_commentRepository = new GenericRepository<Comment>(context)); }
        }

        private GenericRepository<DiaryPermission> _diaryPermissionRepository;

        public GenericRepository<DiaryPermission> DiaryPermissionRepository
        {
            get { return _diaryPermissionRepository ?? (_diaryPermissionRepository = new GenericRepository<DiaryPermission>(context)); }
        }

        private GenericRepository<FriendRequest> _friendRequestRepository;

        public GenericRepository<FriendRequest> FriendRequestRepository
        {
            get { return _friendRequestRepository ?? (_friendRequestRepository = new GenericRepository<FriendRequest>(context)); }
        }

        private GenericRepository<ExtraUserInformation> _extraUserInformationRepository;

        public GenericRepository<ExtraUserInformation> ExtraUserInformationRepository
        {
            get
            {
                return _extraUserInformationRepository ??
                       (_extraUserInformationRepository = new GenericRepository<ExtraUserInformation>(context));
            }
            set { _extraUserInformationRepository = value; }
        }

        private GenericRepository<Medium> _mediumRepository;

        public GenericRepository<Medium> MediumRepository
        {
            get { return _mediumRepository ?? (_mediumRepository = new GenericRepository<Medium>(context)); }
            set { _mediumRepository = value; }
        }

        public void SaveChanges()
        {
            context.SaveChanges();
        }

        private bool _disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this._disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this._disposed = true;
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}